package com.pktechan.api.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pktechan.api.entity.WxPayRefundOrder;
import com.pktechan.api.service.WxPayRefundOrderService;
import com.pktechan.api.utils.HtmlUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @description: 微信支付退款订单功能
 * @author: s.zhang
 * @create: 2021-03-0 10:19
 **/
@RestController
@RequestMapping("wxPayRefundOrderController")
public class WxPayRefundOrderController {
    private static final Logger logger = LoggerFactory.getLogger(WxPayRefundOrderController.class);

    @Autowired
    private WxPayRefundOrderService wxPayRefundOrderService;

    /**
     * 根据id查询微信支付退款订单
     *
     * @param refundOrderId
     * @param response
     */
    @RequestMapping(value = "getWxPayRefundOrderById", method = RequestMethod.POST)
    public void getWxPayRefundOrderById(@RequestParam Integer refundOrderId, HttpServletResponse response) {
        try {
            logger.info("根据id=" + refundOrderId + "查询微信支付退款订单");
            WxPayRefundOrder wxPayRefundOrder = wxPayRefundOrderService.getById(refundOrderId);
            HtmlUtil.writerSuccessJson(response, wxPayRefundOrder);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 微信支付退款订单列表
     *
     * @param wxPayRefundOrder
     * @param response
     */
    @RequestMapping(value = "getWxPayOrderList", method = RequestMethod.POST)
    public void getWxPayOrderList(@RequestBody WxPayRefundOrder wxPayRefundOrder, HttpServletResponse response) {
        try {
            logger.info("微信支付退款订单列表");
            PageHelper.startPage(wxPayRefundOrder.getPageNo(), wxPayRefundOrder.getPageSize());
            List<WxPayRefundOrder> list = wxPayRefundOrderService.getList(wxPayRefundOrder);
            PageInfo pageInfo = new PageInfo(list);
            HtmlUtil.writerSuccessJson(response, pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 添加到微信支付退款订单表
     *
     * @param wxPayRefundOrder
     * @param response
     */
    @RequestMapping(value = "saveWxPayRefundOrder", method = RequestMethod.POST)
    public void saveWxPayRefundOrder(@RequestBody WxPayRefundOrder wxPayRefundOrder, HttpServletResponse response) {
        try {
            logger.info("添加到微信支付退款订单表");
            wxPayRefundOrderService.save(wxPayRefundOrder);
            HtmlUtil.writerSuccessJson(response);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 根据Id修改微信支付退款订单
     *
     * @param wxPayRefundOrder
     * @param response
     */
    @RequestMapping(value = "updateWxPayOrderById", method = RequestMethod.POST)
    public void updateWxPayOrderById(@RequestBody WxPayRefundOrder wxPayRefundOrder, HttpServletResponse response) {
        try {
            logger.info("根据id=" + wxPayRefundOrder.getId() + "修改微信支付退款订单");
            wxPayRefundOrderService.update(wxPayRefundOrder);
            HtmlUtil.writerSuccessJson(response);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

}
